package com.burgos.inventory;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import com.google.gson.Gson;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;


public class Product extends Activity implements OnClickListener {

	
	private ProgressDialog pDialog2;
	private static final String READ_PRODUCT = "http://test1984.site11.com/remote/getproduct.php";
	private String codename;
	private TextView t;
	private Intent i;
	private int posts;
	private int currentpost;
	private String currentcode;
	private String currentID;
	private List<ItemDetails> items;
	private Button updateB;
	private Button nextB;
	private Button uploadB;
		
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.product_description);
		//We get the search string from the previews activity
		codename = getIntent().getExtras().getString("keyword");

		updateB = (Button) findViewById(R.id.update);
		nextB = (Button) findViewById(R.id.next);
		uploadB = (Button) findViewById(R.id.upload);
		updateB.setOnClickListener(this);
		nextB.setOnClickListener(this);
		uploadB.setOnClickListener(this);
	}
	

	@Override
	protected void onResume() {
		super.onResume();
		new loadProduct().execute();
	}
	
	//to handle the buttons: upload/download/next/update
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.next:
			updateList();
			break;
		case R.id.update:
			i = new Intent(this, UpdateStatus.class);
			i.putExtra("keyword",currentcode);
			finish();
			startActivity(i);
			break;
		case R.id.upload:
			i = new Intent(this, UploadFile.class);
			i.putExtra("keyword",currentID);
			i.putExtra("barcode",currentcode);
			finish();
			startActivity(i);
			break;
		default:
			break;
		}
	}
	
	public void getProductDetails() {
			
		
	}
	
	//to show the data on the layout
	private void updateList() {
		if(posts == 0){
		//Nothing to show	
		}
		else {
			t=(TextView)findViewById(R.id.nameP); 
			t.setText("NAME: " + items.get(currentpost).getTAG_NAME());
			t=(TextView)findViewById(R.id.statusP); 
			t.setText("STATUS: " + items.get(currentpost).getTAG_STATUS());
			t=(TextView)findViewById(R.id.lastupdateP); 
			t.setText("LAST UPDATE ON: " + items.get(currentpost).getTAG_LASTUPDATE());
			t=(TextView)findViewById(R.id.barcodeP); 
			t.setText("BARCODE: " + items.get(currentpost).getTAG_BARCODE());
			t=(TextView)findViewById(R.id.clientP); 
			t.setText("CLIENT NAME: " + items.get(currentpost).getTAG_CLIENTNAME());
			t=(TextView)findViewById(R.id.phoneP); 
			t.setText("CLIENT PHONE: " + items.get(currentpost).getTAG_CLIENTNUMBER());
			t=(TextView)findViewById(R.id.qttyP); 
			t.setText("QUANTITY: " + items.get(currentpost).getTAG_QUANTITY());
			t=(TextView)findViewById(R.id.descP); 
			t.setText("DETAILS: " + items.get(currentpost).getTAG_DESCRIPTION());
			
			currentcode =  items.get(currentpost).getTAG_BARCODE();
			currentID =  items.get(currentpost).getTAG_ID();
			
			//to hanlde the NEXT button if there is more than 1 product matching the searching criteria
			if(currentpost == (posts-1)){
				//end of circular list
				currentpost = 0;
			}
			else{
				//to show the next product of the query
				currentpost++;
			}
		}
	}
	
	//Class to make the http request and get the product data
	public class loadProduct extends AsyncTask<String, String, String> {
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			pDialog2 = new ProgressDialog(Product.this);
			pDialog2.setMessage("Retrieving Product Info...");
			pDialog2.setIndeterminate(false);
			pDialog2.setCancelable(true);
			pDialog2.show();
		}

		@Override
		protected String doInBackground(String... arg0) {
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			params.add(new BasicNameValuePair("keyword", codename));

			JSONParser jsonParser = new JSONParser();
			JSONObject json = jsonParser.makeHttpRequest(READ_PRODUCT, "POST", params);
			String det = json.toString();
			Log.i("test1","response from http");
			//Using GSON to build the list of products
			Gson gson = new Gson();
		    ListofItem data = gson.fromJson(det, ListofItem.class);
		    Log.i("test2","crrate lists");
		    items = data.getProducts();
		    posts = data.getProducts().size();
		    //reset to view first product of the list
		    currentpost = 0;
			return null;
		}

		@Override
		protected void onPostExecute(String result) {
			pDialog2.dismiss();
			updateList();
		}
	}
}
